package com.tads.torry.messagecenter.domain.accessor.impl;

import com.tads.torry.messagecenter.domain.enums.ReceiverType;
import com.tads.torry.messagecenter.infras.entity.MessageTemplateDO;
import com.tads.torry.messagecenter.domain.model.MessageTemplate;
import com.tads.torry.messagecenter.infras.mapper.MessageTemplateMapper;
import com.tads.torry.messagecenter.domain.accessor.MessageTemplateAccessor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.zen.boot.starter.mybatisplus.service.AccessorImpl;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author torry.wang
 * @since 2025-03-12
 */
@Service
@SuppressWarnings("DuplicatedCode")
public class MessageTemplateAccessorImpl extends AccessorImpl<MessageTemplateMapper, MessageTemplateDO, MessageTemplate> implements MessageTemplateAccessor {
    @Override
    public MessageTemplate toModel(MessageTemplateDO entity) {
        if (entity == null) return null;
        MessageTemplate messageTemplate = new MessageTemplate();
        messageTemplate.setTenantId(entity.getTenantId());
        messageTemplate.setDefaultSenderId(entity.getDefaultSenderId());
        messageTemplate.setDescr(entity.getDescr());
        messageTemplate.setTitle(entity.getTitle());
        messageTemplate.setContent(entity.getContent());
        messageTemplate.setDefaultReceivers(StringUtils.isNotBlank(entity.getDefaultReceivers()) ?
                new ArrayList<>(Arrays.asList(entity.getDefaultReceivers().split(","))) : null);

        messageTemplate.setDefaultReceiverType(StringUtils.isNotBlank(entity.getDefaultReceiverType()) ? ReceiverType.valueOf(entity.getDefaultReceiverType()) : null);
        messageTemplate.setId(entity.getId());
        messageTemplate.setCreateTime(entity.getCreateTime());
        messageTemplate.setUpdateTime(entity.getUpdateTime());
        return messageTemplate;
    }

    @Override
    public MessageTemplateDO toEntity(MessageTemplate model) {
        if (model == null) return null;

        MessageTemplateDO messageTemplateDO = new MessageTemplateDO();
        messageTemplateDO.setTenantId(model.getTenantId());
        messageTemplateDO.setDefaultSenderId(model.getDefaultSenderId());
        messageTemplateDO.setDescr(model.getDescr());
        messageTemplateDO.setTitle(model.getTitle());
        messageTemplateDO.setContent(model.getContent());
        messageTemplateDO.setDefaultReceivers(CollectionUtils.isEmpty(model.getDefaultReceivers()) ? null : String.join(",", model.getDefaultReceivers()));
        messageTemplateDO.setDefaultReceiverType(model.getDefaultReceiverType() == null ? null : model.getDefaultReceiverType().name());
        messageTemplateDO.setId(model.getId());
        messageTemplateDO.setCreateTime(model.getCreateTime());
        messageTemplateDO.setUpdateTime(model.getUpdateTime());
        return messageTemplateDO;
    }
}
